package org.javacore.collection.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
/*
 * Copyright [2015] [Jeff Lee]
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @author Jeff Lee
 * @since 2015-6-17 23:32:30
 * 	Collections的使用
 */
public class CollectionsT{
	public static void main(String[] args){
		// 测试集合工具类Sort方法
		testSort();
		testAddAll();
        testMapSort();
        testValueOfMapSort();
	}

	/**
	 * 测试集合工具类Sort方法
	 */
private static void testSort(){
	List<Integer> list = new ArrayList<Integer>();
	list.add(1);
	list.add(3);
	list.add(2);
	list.add(4);
	// 调用集合工具类Sort方法
	Collections.sort(list);

	System.out.println("list sorted:");
	System.out.print("\t" + list + "\n");
}

private static void testAddAll() {
	List<String> list = new ArrayList<>();
	list.add("s2");
	list.add("s4");
	list.add("s1");
	list.add("s3");
	System.out.println(list);
	Collections.addAll(list, "s5","s7","s8","s9");
    System.out.println(list);
    Collections.sort(list, new Comparator<Object>() {
        @Override
        //按上传时间降序排
        public int compare(Object o1, Object o2) {
            /*Integer s1 = Integer.valueOf(((String) o1).substring(1));
            Integer s2 = Integer.valueOf(((String) o2).substring(1));*/
            String s1 = (String) o1;
            String s2 = (String) o2;
            if(s1.compareTo(s2) < 0)
                return 1;
            else if (s1.compareTo(s2) > 0) {
                return -1;
            }
            return 0;
        }
    });
	System.out.println(list);
}
    public static void testMapSort(){
        Map map = new HashMap();
        map.put("1", "1");
        map.put("4", "4");
        map.put("2", "2");
        map.put("2", "3");
        map.put("3", "3");
        // 注意排序
        Map m = new TreeMap();
        m.putAll(map);
        System.out.println(m);
    }

    public static void testValueOfMapSort(){
        Map<String,String> map = new HashMap();
        map.put("1", "1");
        map.put("4", "4");
        map.put("2", "2");
        map.put("2", "3");
        map.put("3", "3");
        // 注意排序
        //Map m = sortByValue(map);
        Set s = map.entrySet();
        System.out.println(s);
        for (Map.Entry ma : map.entrySet()) {
            System.out.println("key:"+ma.getKey()+" value:"+ma.getValue());
        }
    }

    public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue( Map<K, V> map ) {
        List<Map.Entry<K, V>> list =
            new LinkedList<Map.Entry<K, V>>( map.entrySet() );
        Collections.sort( list, new Comparator<Map.Entry<K, V>>()
        {
            public int compare( Map.Entry<K, V> o1, Map.Entry<K, V> o2 )
            {
                return (o1.getValue()).compareTo( o2.getValue() );
            }
        } );

        Map<K, V> result = new LinkedHashMap<K, V>();
        for (Map.Entry<K, V> entry : list)
        {
            result.put( entry.getKey(), entry.getValue() );
        }
        return result;
    }
}
